﻿//-----------------------------------------------------------------------
// <copyright file="FrameworkTimer.cs" author="Artak Mkrtchyan">
//     Creative Commons - GNU General Public License (http://creativecommons.org/licenses/GPL/2.0/
// </copyright>
// <author>Artak Mkrtchyan</author>
// <email>mkArtak@gmail.com</email>
// <date>29/03/2013</date>
//-----------------------------------------------------------------------
using System;
using System.Threading;

namespace ConsoleHoster.Common.Utilities.Threading
{
	public class FrameworkTimer : ITimer
	{
		public event EventHandler Tick;

		private readonly Timer timer;
		private TimeSpan interval;

		public FrameworkTimer(TimeSpan argInterval)
		{
			this.timer = new Timer(new TimerCallback(this.OnTimerCallback));
			this.Interval = argInterval;
		}

		public void Start()
		{
			this.timer.Change(this.Interval, this.Interval);
		}

		public void Stop()
		{
			this.timer.Change(Timeout.Infinite, Timeout.Infinite);
		}

		public void Dispose()
		{
			this.timer.Dispose();
		}

		private void OnTimerCallback(object state)
		{
			EventHandler tmpEH = this.Tick;
			if (tmpEH != null)
			{
				tmpEH(this, EventArgs.Empty);
			}
		}

		public TimeSpan Interval
		{
			get
			{
				return this.interval;
			}
			set
			{
				this.interval = value;
			}
		}
	}
}
